In [1]:
import tensorflow as tf

In [7]:
x = tf.add(5,2)
print(x)
with tf.Session() as sess:
    output = sess.run(x)
    print(output)


Tensor("Add_4:0", shape=(), dtype=int32)
7

In [10]:
x2 = tf.subtract(10,4)
y2 = tf.multiply(2,5)

print(x2, y2)

with tf.Session() as sess:
    print(sess.run(x2))
    print(sess.run(y2))
    print(sess.run(x2) + sess.run(y2))


Tensor("Sub_2:0", shape=(), dtype=int32) Tensor("Mul_2:0", shape=(), dtype=int32)
6
10
16

Converting types


In [11]:
tf.subtract(tf.constant(2.0),tf.constant(1))  # Fails with ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    489                 as_ref=input_arg.is_ref,
--> 490                 preferred_dtype=default_dtype)
    491           except TypeError as err:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    740         if ret is None:
--> 741           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    742 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
    613         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 614         % (dtype.name, t.dtype.name, str(t)))
    615   return t

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("Const_1:0", shape=(), dtype=int32)'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-b91adb415f76> in <module>()
----> 1 tf.subtract(tf.constant(2.0),tf.constant(1))  # Fails with ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in subtract(x, y, name)
    303 
    304 def subtract(x, y, name=None):
--> 305   return gen_math_ops._sub(x, y, name)
    306 
    307 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py in _sub(x, y, name)
   2499     A `Tensor`. Has the same type as `x`.
   2500   """
-> 2501   result = _op_def_lib.apply_op("Sub", x=x, y=y, name=name)
   2502   return result
   2503 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    524                   "%s type %s of argument '%s'." %
    525                   (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name,
--> 526                    inferred_from[input_arg.type_attr]))
    527 
    528           types = [values.dtype]

TypeError: Input 'y' of 'Sub' Op has type int32 that does not match type float32 of argument 'x'.

In [13]:
x3 = tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1))   # 1
with tf.Session() as sess:
    print(sess.run(x3))


1

Quiz


In [15]:
# Solution is available in the other "solution.py" tab
import tensorflow as tf

# TODO: Convert the following to TensorFlow:
x = 10
y = 2
z = x/y - 1

# TODO: Print z from a session

z = tf.subtract(tf.divide(10, 2),1)
with tf.Session() as sess:
    print(sess.run(z))


4.0

In [16]:
# Quiz Solution
# Note: You can't run code in this tab
import tensorflow as tf

# TODO: Convert the following to TensorFlow:
x = tf.constant(10)
y = tf.constant(2)
z = tf.subtract(tf.divide(x,y),tf.cast(tf.constant(1), tf.float64))

# TODO: Print z from a session
with tf.Session() as sess:
    output = sess.run(z)
    print(output)


4.0

In [ ]: